package study06;

public class Java06_Thread_sync {
    public static void main (String arg[]) throws Exception {
//        线程 - 同步
//        关键字  synchronized
        Num num = new Num();

        User user = new User(num);
        user.start();

        Bank bank = new Bank(num);
        bank.start();
    }
}

class Num {

}

class Bank extends Thread {
    private Num num;
    public Bank (Num num) {
        this.num = num;
    }
    public void run() {
        synchronized (num) {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

            System.out.println("开始上班，开始叫号");
            num.notifyAll();
        }
    }
}

class  User extends Thread {
    private Num num;
    public User (Num num) {
        this.num = num;
    }
    public void run() {
        synchronized (num) {
            System.out.println("我是号码1，银行还没开门我需要等一会");

            try {
                num.wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

            System.out.println("叫到我了，开始办理业务");
        }
    }
}


